package cc.momas.mybatis.multidatasource.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Sod-Momas
 * @since 2019.11.04
 **/
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {

//    @Bean("mysqlDataSource")
//    @Qualifier("mysqlDataSource")
//    @ConfigurationProperties(prefix = "spring.datasource")
//    public DataSource mysqlDataSource() {
//        return DataSourceBuilder.create().build();
//    }

//    @Bean
//    @Qualifier("superviseDataSource")
////    @ConfigurationProperties(prefix = "spring.datasource.supervise")
//    public DataSource superviseDataSource() {
//        return DataSourceBuilder.create()
//                .password("mysql8")
//                .username("root")
//                .url("jdbc:mysql://localhost/supervise?UseUnicode=true&serverTimezone=UTC")
//                .driverClassName("com.mysql.cj.jdbc.Driver")
//                .build();
//    }

//    @Bean
//    @Primary
//    public DynamicDataSource dataSource(@Qualifier("mysqlDataSource") DataSource mysql,
//                                        @Qualifier("superviseDataSource") DataSource supervise) {
//        Map<Object, Object> targetDataSources = new HashMap<>();
//        targetDataSources.put(DatabaseType.MYSQL, mysql);
//        targetDataSources.put(DatabaseType.SUPERVISE, supervise);
//
//        DynamicDataSource dataSource = new DynamicDataSource();
//        dataSource.setTargetDataSources(targetDataSources);// 该方法是AbstractRoutingDataSource的方法
//        dataSource.setDefaultTargetDataSource(mysql);// 默认的datasource设置为myTestDbDataSource
//
//        return dataSource;
//    }
//
//
//    private static final String MAPPER_LOCATION = "mybatis.mapper-locations.db1";
//
//    @Autowired
//    private Environment env;
//
//    @Bean
//    @ConfigurationProperties(prefix = "mybatis.configuration")
//    @Scope("prototype")
//    public org.apache.ibatis.session.Configuration globalConfiguration() {
//        return new org.apache.ibatis.session.Configuration();
//    }
//
//    @Bean(name = "db1SqlSessionFactory")
//    @Primary
//    public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource myTestDbDataSource,
//                                               org.apache.ibatis.session.Configuration config) throws Exception {
//        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
//        fb.setDataSource(myTestDbDataSource);
//        fb.setVfs(SpringBootVFS.class);
//        fb.setConfiguration(config);
//        fb.setTypeAliasesPackage(env.getProperty("mybatis.type-aliases-package"));
//        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty(MAPPER_LOCATION)));
//        return fb.getObject();
//    }
//
//
//    @Bean("masterTransactionManager")
//    public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource myTestDbDataSource) {
//        return new DataSourceTransactionManager(myTestDbDataSource);
//    }
}
